package com.sy.wechat.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sy.model.wechat.Menu;

import com.sy.vo.wechat.MenuVo;
import com.sy.wechat.mapper.MenuMapper;
import com.sy.wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-05-05
 */
@Service
public class MenuServiceImp extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    WxMpService wxMpService;
    @Override
    public List<MenuVo> findMenuInfo() {
        // 1. 查询表中的所有菜单
        List<Menu> menus = this.getBaseMapper().selectList(null);
        // 2. 先筛选出一级菜单
        // 封装其对应的二级菜单，parentId和id进行比较
        List<Menu> menuList = menus.stream().filter(m -> m.getParentId().longValue() == 0).collect(Collectors.toList());
        List<MenuVo> menus1 = new ArrayList<>();
        for (Menu m : menuList){
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(m,menuVo);
            // 3. 获得二级菜单
            List<MenuVo> menus2 = menus.stream()
                    .filter(menu -> m.getId().longValue() == menu.getParentId().longValue())
                    .map(menu -> {
                        MenuVo menuVo1 = new MenuVo();
                        BeanUtils.copyProperties(menu, menuVo1);
                        return menuVo1;
                    })
                    .collect(Collectors.toList());
            // 4. 封装
            menuVo.setChildren(menus2);
            menus1.add(menuVo);
        }

        return menus1;
    }

    // 同步菜单
    @Override
    public void syncMenu() {
        List<MenuVo> menuVoList = this.findMenuInfo();
        // 封装菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://8gqsyp.natappfree.cc/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://8gqsyp.natappfree.cc#"+twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        JSONObject button = new JSONObject();
        button.put("button", buttonList);

        // 完成同步
        try {
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @SneakyThrows
    @Override
    public void removeMenu() {
        wxMpService.getMenuService().menuDelete();
    }

}
